package myky.com.doctorapp.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_follow_html.*
import kotlinx.android.synthetic.main.activity_follow_record_list.*
import kotlinx.android.synthetic.main.public_common_title.*

import myky.com.doctorapp.R
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.FollowHtmlActivityContract
import myky.com.doctorapp.entity.FollowRecordEntity
import myky.com.doctorapp.entity.FollowUploadEntity
import myky.com.doctorapp.presenter.FollowHtmlActivityPresenter
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.HtmlUtils

/**
 * Description: 随访问诊表
 * @author wuyang
 * Created by 2019/3/20 16:35
 */
class FollowHtmlActivity : BaseActivity<FollowHtmlActivityPresenter, FollowUploadEntity.DataBean>(),FollowHtmlActivityContract.View {


    override fun getPresenter(): FollowHtmlActivityPresenter = FollowHtmlActivityPresenter()

    override fun showDataSuccess(datas: FollowUploadEntity.DataBean) {

        if (datas!=null){
            var intent=Intent()
            intent.putExtra(Constants.RESULTDATE, datas.inputtime)
            setResult(Activity.RESULT_OK,intent)
            finish()
        }
    }


    override fun reloadActivity() {

    }

    override fun injectTarget(): View=findViewById(R.id.follow_html_lin)


    override fun initData() {
        follow_html_web.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress)
                if (follow_html_bar != null) {
                    if (newProgress == 100) {
                        follow_html_bar.visibility = View.GONE
                        //progressBar.setProgress(newProgress);
                    } else {
                        follow_html_bar.visibility = View.VISIBLE
                        //设置加载进度
                        follow_html_bar.progress = newProgress
                    }
                }
            }
        }

        /**
         * js交互
         */
        HtmlUtils.js_java { data -> mPresenter.puploadFollowHtml(data) }
    }

    override fun initView() {
        var bundle = intent.extras
        var bean = bundle.getSerializable(Constants.INTENTENTITY) as FollowRecordEntity.DataBean.FollowrecordlistBean
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.follow_html_title)
                .init()
        conmonTitleTextView.text=bean.title

        HtmlUtils.setjsInterface(this, follow_html_web, null)
        val id = bean.userquestionnairehistoryid
        val htmlVersion = bean.htmlVersion
        val url = Address.FOLLOWURL + "questionId=" + id + "&version=" + htmlVersion
        follow_html_web.loadUrl(url)

        backLayout.setOnClickListener { finish() }
    }


    override fun getContenView(): Int =R.layout.activity_follow_html

}
